/*
 * Copyright (C) 2004, Velocityme Partnership
 * TaskValueActionForm.java
 *
 * Created on 12 September 2004, 10:14
 */

package com.velocityme.www.actionforms.nodeactionforms;

import com.velocityme.entity.Contactable;
import com.velocityme.entity.Task;
import com.velocityme.session.ContactableLocal;
import com.velocityme.session.KeyLocal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author  Robert
 */
public class TaskActionForm extends NodeActionForm {
    
    private long m_ownerId;
    private int m_priority = 50;
    private boolean interested = false;
    private Long[] m_allInterestedContactableIds;
    private Long[] m_interestedContactableIds;
    private Long[] m_allResponsibleContactableIds;
    private Long[] m_responsibleContactableIds;

    public void extractTask(KeyLocal key, Task task) {
        extractNode(task);
        ContactableLocal contactableLocal = lookupContactableBean();
        task.setOwner(contactableLocal.findContactable(key, m_ownerId));
        task.setPriority(m_priority);
        if (interested) {
            task.addInterestedContactable(key.getPerson());
        }
        task.clearInterestedContactables();
        if (m_interestedContactableIds != null) {
            for (Long id: m_interestedContactableIds) {
                Contactable contactable = contactableLocal.findContactable(key, id);
                task.addInterestedContactable(contactable);
            }
        }
        task.clearResponsibleContactables();
        if (m_responsibleContactableIds != null) {
            for (Long id: m_responsibleContactableIds) {
                Contactable contactable = contactableLocal.findContactable(key, id);
                task.addResponsibleContactable(contactable);
            }
        }
    }

    public long getOwnerId() {
        return m_ownerId;
    }
    
    public void setOwnerId(long ownerId) {
        m_ownerId = ownerId;
    }
    
    public int getPriority() {
        return m_priority;
    }
    
    public void setPriority(int priority) {
        m_priority = priority;
    }
    
    public boolean isInterested() {
        return interested;
    }
    
    public void setInterested(boolean interested) {
        this.interested = interested;
    }
    
    public Long[] getAllInterestedContactableIds() {
        return m_allInterestedContactableIds;
    }
    
    public void setAllInterestedContactableIds(Long[] allInterestedContactableIds) {
        m_allInterestedContactableIds = allInterestedContactableIds;
    }
    
    public Long[] getInterestedContactableIds() {
        return m_interestedContactableIds;
    }
    
    public void setInterestedContactableIds(Long[] interestedContactableIds) {
        m_interestedContactableIds = interestedContactableIds;
    }
    
    public Long[] getAllResponsibleContactableIds() {
        return m_allResponsibleContactableIds;
    }
    
    public void setAllResponsibleContactableIds(Long[] allResponsibleContactableIds) {
        m_allResponsibleContactableIds = allResponsibleContactableIds;
    }
    
    public Long[] getResponsibleContactableIds() {
        return m_responsibleContactableIds;
    }
    
    public void setResponsibleContactableIds(Long[] responsibleContactableIds) {
        m_responsibleContactableIds = responsibleContactableIds;
    }
    
    private ContactableLocal lookupContactableBean() {
        try {
            Context c = new InitialContext();
            return (ContactableLocal) c.lookup("java:comp/env/ContactableBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
